<?php

class Talktangle_Plugin_Core extends Zend_Controller_Plugin_Abstract {
	public function onTanglePaymentReturn($event)
	  {
	    
		$payload = $event->getPayload();
        $payload = $payload['for'];
        $viewer = Engine_Api::_()->user()->getViewer();

		if( $payload instanceof Payment_Model_Order ) {
            $source = $payload->getSource();
            $source->status = $payload->state;
            $tangle = $source->getSource();
            
            if($tangle) {
                $tangle->payment_status = $payload->state;
                $source->save();
                $tangle->save();
               
                $actionT = Engine_Api::_()->getDbtable('actions','activity');
                $action = $actionT->addActivity($viewer,$tangle,$tangle->getActivityType());
                Engine_Api::_()->getDbtable('actions', 'activity')->attachActivity($action, $tangle);
                
                $point =  Engine_Api::_()->getApi('settings', 'core')->getSetting('talktangle.point', 2);
                $viewer->points = $viewer->points + $point;
                $viewer->tangle = $viewer->tangle + 1;
                $viewer->save();
                
                $core = Engine_Api::_()->talktangle();
                if ( !empty($tangle->facebook) && $core->enableFacebook() ) {
                    $params = array(
                        'body' => 'had post new tangle '.$tangle->title,
                        'link' => 'http://' . $_SERVER['HTTP_HOST'].$tangle->getHref()
                         
                    );
                    $core->postFacebook($params);
                }
                if ( !empty($tangle->tiwtter)  && $core->enableTiwtter()) {
                    $params = array(
                        'body' => 'had post new tangle '.$tangle->title,
                        'link' => 'http://' . $_SERVER['HTTP_HOST'].$cross->getHref()
                         
                    );
                    $core->postTwitter($params);
                }
             
            }
            
		}
         
	  }
}